package graph;

import java.util.LinkedList;

import bookworm.ParallelThreadsCounter;

/**
 * for a new node added to the graph, this thread will add the edges for this node 
 * based on related authors
 * @author Mina
 *
 */
public class ThreadCreateEdgesOfGraphBasedOnAuthors extends Thread
{
	private BookReviewNode newNode;
	private BookReviewGraphInterface graph;
	
	public ThreadCreateEdgesOfGraphBasedOnAuthors(BookReviewNode newNode,
															BookReviewGraphInterface graph)
	{
		this.newNode = newNode;
		this.graph = graph;
	}
	
	public void run()
	{
		this.createNewIncidentEdgesForNewNodeBasedOnAuthors(newNode);
	}
	
	private void createNewIncidentEdgesForNewNodeBasedOnAuthors(BookReviewNode newNode)
	{
		LinkedList<String> authorsFromNewNode = newNode.getReviewData().getAuthors();
		for(int i = 0; i < authorsFromNewNode.size(); i++)
		{
			String authorFromNewNode = authorsFromNewNode.get(i);
			/*after we get an author, we'll get the starting node on graph 
			 * based on this author(if there was any). Then, we'll add a new Edge between this
			 * starting node and the new node. And we'll also add a new Edge between
			 * the new node and the neighbors(not all of them) of the starting node.
			 * By doing this, the new node will be connected with all other nodes that have
			 * the same author.
			 */
			
			AdministratorOfAllStartingNodes administratorOfStartingNodes =
					AdministratorOfAllStartingNodes.getAdministratorOfAllStartingNodes();
			BookReviewNode startingNodeFromAuthor = 
					administratorOfStartingNodes.getStartingNodeForAuthor(authorFromNewNode);
			
			
			if(startingNodeFromAuthor != null && 
					newNode.isAlreadyConnectedTo(startingNodeFromAuthor) == false
						&& startingNodeFromAuthor != newNode)
			{
				//there isn't an edge between these nodes and they are different nodes
				graph.createNewEdge(newNode, startingNodeFromAuthor);
				//we need to add a new edge to some of the neighbors of the starting node :
				//the ones with the same author of the new node.
				this.addEdgesFromNewNodeToStartingNodeNeighbors(newNode, startingNodeFromAuthor);
			}
		}
		
		ParallelThreadsCounter countsActiveThreads = 
				ParallelThreadsCounter.getParallelThreadsCounter();
		countsActiveThreads.decreaseCounterThreadsActive();
		//thread finished its work
	}

	private void addEdgesFromNewNodeToStartingNodeNeighbors(BookReviewNode newNode,
														BookReviewNode startingNodeFromAuthor) 
	{
		LinkedList<BookReviewNode> startingNodeNeighbors = 
									startingNodeFromAuthor.getNeighborsOfNode();
		for(int j = 0; j < startingNodeNeighbors.size(); j++)
		{
			BookReviewNode startingNodeNeighbor = startingNodeNeighbors.get(j);
			if(newNode.shareOneSameAuthor(startingNodeNeighbor) == true)
			{
				/*they share the same author? then there must be an edge between them!
				  Is there already an Edge between them?
				*/
				if(newNode.isAlreadyConnectedTo(startingNodeNeighbor) == false)
				{
					graph.createNewEdge(newNode, startingNodeNeighbor);
				}
			}
		}
	}
	
	

}
